﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using DunSilverlight.Comm;
using DunLibrary.User;
using Telerik.Windows.Controls;

namespace DunSilverlight.Views
{
    public partial class RightPage : Page
    {
        RightService.RightServiceClient ser = new RightService.RightServiceClient();
        public RightPage()
        {
            if (!RightManagement.VerifyRight(RightManagement.Right_Right_Manage))
            {
                if (AppGlobal.CurrentUser != null)
                    Tools.ShowMessage("该用户没有权限查看 ", "", false);
                return;
            }
            InitializeComponent();
            ser.GetListByHQLCompleted += new EventHandler<RightService.GetListByHQLCompletedEventArgs>(ser_GetListByHQLCompleted);
            ser.DeleteByIdCompleted += new EventHandler<RightService.DeleteByIdCompletedEventArgs>(ser_DeleteByIdCompleted);
            Loaded += new RoutedEventHandler(RightPage_Loaded);
        }

       

       

        void RightPage_Loaded(object sender, RoutedEventArgs e)
        {
            Query();
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            Query();
        }

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        /// <summary>
        /// 执行查询
        /// 基础资料都比较少，不需要分页，也没有查询条件
        /// </summary>
        private void Query()
        {
            ser.GetListByHQLAsync("from Right");
        }

        void ser_GetListByHQLCompleted(object sender, RightService.GetListByHQLCompletedEventArgs e)
        {
            int totalCount = 0;
            IList<DunLibrary.User.Right> list = JsonSerializerHelper.JsonToEntities<DunLibrary.User.Right>(e.Result, out totalCount);

            dgdBank.ItemsSource = list;
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            RightForm form = new RightForm();
            Tools.OpenWindow("新建权限", form, null, new Action(() =>
            {
                Query();
            }));
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            Right right = dgdBank.SelectedItem as Right;

            if (right != null)
            {
                RightForm frmBank = new RightForm(right);
                Tools.OpenWindow("编辑权限", frmBank, null, new Action(() => Query()));
            }

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dgdBank.SelectedItem != null)
            {
                DunLibrary.User.Right right = (dgdBank.SelectedItem as DunLibrary.User.Right);
                EventHandler<WindowClosedEventArgs> callback = new EventHandler<WindowClosedEventArgs>(
                    (o, args) => { if (args.DialogResult == true) ser.DeleteByIdAsync(right.Id); });
                Tools.OpenConfirm("删除该权限将影响具有该权限的角色！确定要删除吗？", callback);
            }

            
        }

        void ser_DeleteByIdCompleted(object sender, RightService.DeleteByIdCompletedEventArgs e)
        {
            Query();
        }
    }
}
